<template>
  <div class='ele-body ele-body-card'>
    <!-- <a-divider>服务器连接设置(暂时不支持)</a-divider>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='ipPortformRef'
        :model='ipPortformModel'
        :rules="ipPortRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='setIpPortActionToCmdQueue'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='log反馈的http地址:' name='httpUrl'>
              <a-input v-model:value='ipPortformModel.httpUrl' placeholder='请输入log反馈的http地址' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='mqtt地址:' name='mqttUrl'>
              <a-input v-model:value='ipPortformModel.mqttUrl' placeholder='请输入mqtt地址' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='mqtt端口号:' name='mqttPort'>
              <a-input v-model:value='ipPortformModel.mqttPort' placeholder='请输入mqtt端口号，例如8000' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='请输入mqtt账号:' name='username'>
              <a-input v-model:value='ipPortformModel.username' placeholder='请输入mqtt账号' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='请输入mqtt密码:' name='pwd'>
              <a-input v-model:value='ipPortformModel.pwd' placeholder='请输入mqtt密码' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading='ipPortformRefLoading'>
              推送到相机
            </a-button>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card> -->

    <a-divider>图像拉取缩放比例设置</a-divider>
    <a-alert message="默认的配置" type="warning" />
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='captureimgFormRef'
        :model='captureimgFormModel'
        :rules="captureimgFormRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='setCaptureimgScaleActionToCmdQueue'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='图像缩放比例:' name='captureimgScale'>
              <a-input v-model:value='captureimgFormModel.captureimgScale' placeholder='请输入图像缩放比例' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='图像质量:' name='captureimgQuality'>
              <a-input v-model:value='captureimgFormModel.captureimgQuality' placeholder='请输入图像质量' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='是否携带标注槽位:' name='captureimgMarks'>
              <a-select v-model:value="captureimgFormModel.captureimgMarks">
                <a-select-option :value="0">否</a-select-option>
                <a-select-option :value="1">是</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='是否携带识别检测结果:' name='captureimgCheck'>
              <a-select v-model:value="captureimgFormModel.captureimgCheck">
                <a-select-option :value="0">否</a-select-option>
                <a-select-option :value="1">是</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='是否携带时间:' name='captureimgTime'>
              <a-select v-model:value="captureimgFormModel.captureimgTime">
                <a-select-option :value="0">否</a-select-option>
                <a-select-option :value="1">是</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_IMAGE_SETTING_BUTTON']">
              保存到数据库
            </a-button>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>

    <a-divider>WIFI热点设置</a-divider>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='wifiHotspotFormRef'
        :model='wifiHotspotFormModel'
        :rules="wifiHotspotFormRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='saveWifiHotspotConfig'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='WIFI热点名称:' name='spray'>
              <a-input v-model:value='wifiHotspotFormModel.ssid' placeholder='请输入WIFI热点名称' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='WIFI热点密码:' name='identify'>
              <a-input v-model:value='wifiHotspotFormModel.pwd' placeholder='请输入WIFI热点密码' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading="wifiHotspotFormRefLoading" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_WIFI_SETTING_BUTTON']">
              同步到相机
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>

          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>


    <a-divider>心跳设置</a-divider>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='heartbeatFormRef'
        :model='heartbeatFormModel'
        :rules="heartbeatFormRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='saveHeartbeatConfig'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='心跳时间:' name='heartbeat'>
              <a-input v-model:value='heartbeatFormModel.heartIntervalTime' placeholder='请输入心跳时间' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>
         <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='数据上报周期:' name='dataDuration'>
              <a-input v-model:value='heartbeatFormModel.dataDuration' placeholder='请输入数据上报周期' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading="heartbeatFormRefLoading" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_HEARTBEAT_SET_BUTTONG']">
              同步到相机
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' @click="syncHeartbeatConfigToSomeOxtall()" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_HEARTBEAT_SETBYOXTALL_BUTTONG']">
              同步到相同牛舍下的所有设备
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' @click="readHeartbeatConfig()" :loading="readHeartbeatFormRefLoading" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_HEARTBEAT_READ_BUTTONG']">
              读取相机心跳配置
            </a-button>
          </a-col>
          <a-col :md='7' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>


    <a-divider>全局操作</a-divider>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
      >
        <a-row :gutter='8'>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' @click="rebootActionToCmdQueue()" :loading='rebootRefLoading' v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_RESTART_BUTTONG']">
              重启相机
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>

          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>


    <a-divider>图片传输设置</a-divider>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='imageUploadFormRef'
        :model='imageUploadFormModel'
        :rules="imageUploadFormRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='saveImageUploadForm'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='文件上传模式:' name='uploadMode'>
              <a-select v-model:value='imageUploadFormModel.mode' placeholder='请选择文件上传模式'>
                <a-select-option value="0">请选择</a-select-option>
                <a-select-option value="bin">bin(二进制转base64上传)</a-select-option>
                <a-select-option value="oss">OSS直传模式</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8' v-if="imageUploadFormModel.mode == 'oss'">
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='OSS配置AK:' name='ossAk'>
              <a-input v-model:value='imageUploadFormModel.ossAk' placeholder='请输入OSS配置AK' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8' v-if="imageUploadFormModel.mode == 'oss'">
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='OSS配置SK:' name='ossSk'>
              <a-input v-model:value='imageUploadFormModel.ossSk' placeholder='请输入OSS配置SK' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

         <a-row :gutter='8' v-if="imageUploadFormModel.mode == 'oss'">
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='OSS配置ossBucket:' name='ossBucket'>
              <a-input v-model:value='imageUploadFormModel.ossBucket' placeholder='请输入OSS配置ossBucket' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8' v-if="imageUploadFormModel.mode == 'oss'">
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='OSS配置路径前缀:' name='ossFileprefix'>
              <a-input v-model:value='imageUploadFormModel.ossFileprefix' placeholder='请输入OSS配置路径前缀' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

        <a-row :gutter='8' v-if="imageUploadFormModel.mode == 'oss'">
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='OSS配置ossEndpoint:' name='ossEndpoint'>
              <a-input v-model:value='imageUploadFormModel.ossEndpoint' placeholder='请输入OSS配置的ossEndpoint' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24'/>
        </a-row>

      
        <a-row :gutter='8'>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading="imageUploadFormRefLoading" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_SET_IMG_UPLOAD_BUTTONG']">
              同步到相机
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' @click="syncUploadConfigToSomeOxtall()" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_SETBYOXTALL_IMG_UPLOAD_BUTTONG']">
              同步到相同牛舍下的所有设备
            </a-button>
          </a-col>
          <a-col :md='5' :sm='24' :xs='24'>
            <a-button type='primary' @click="readImageUploadConfig()" :loading="readImageUploadFormRefLoading">
              读取相机图片传输配置
            </a-button>
          </a-col>
          <a-col :md='7' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>

  </div>
</template>

<script>
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import { message } from 'ant-design-vue';
import {DeviceSprayCameraStaticConfigApi} from "@/api/property/spraycamera/DeviceSprayCameraStaticConfigApi";
import {DeviceCommandTemplateDistributeApi} from "@/api/property/DeviceCommandTemplateDistributeApi";
export default {
  name: 'DeviceSpraycameraDomainImgConfig',
  components: {
  },
  props: {
    data: Object
  },
  async created() {
    await this.init();
  },
  data() {
    return {
      loading : false,

      sourceConfig : {},
      ipPortformModel: {},
      ipPortRules : {},
      ipPortformRefLoading: false,
      ipPortformRefTimer : undefined,

      captureimgFormModel: {},
      captureimgFormRules : {},
      captureimgFormRefLoading: false,
      captureimgFormRefTimer : undefined,

      sprayFormModel: {},
      sprayFormRules: {},
      sprayFormRefLoading: false,
      sprayFormRefTimer: undefined,

      wifiHotspotFormModel : {},
      wifiHotspotFormRules : {},
      wifiHotspotFormRefLoading: false,
      wifiHotspotFormRefTimer : undefined,

      rebootRefLoading : false,
      rebootRefTimer : undefined,

      sprayModeModel : {},
      sprayModeRules : {},
      sprayModeRefLoading : false,
      sprayModeRefTimer : undefined,
      spraymode : 0,

      imageUploadFormModel : {},
      imageUploadFormRules : {},
      imageUploadFormRefLoading: false,
      imageUploadFormRefTimer : undefined,

      heartbeatFormModel : {},
      heartbeatFormRules : {},
      heartbeatFormRefLoading: false,
      heartbeatFormRefTimer : undefined,

      readHeartbeatFormRefLoading : false,
      readHeartbeatFormRefTimer : undefined,

      readImageUploadFormRefLoading : false,
      readImageUploadFormRefTimer : undefined,
    };
  },
  methods: {
    async init() {
      let _conf = await DeviceSprayCameraStaticConfigApi.detail({'recordId': this.data.recordId});
      if (_conf) {
        this.sourceConfig = _conf;
        this.ipPortformModel.recordId = _conf.recordId;
        this.ipPortformModel.httpUrl = _conf.httpUrl;
        this.ipPortformModel.mqttUrl = _conf.mqttUrl;
        this.ipPortformModel.mqttPort = _conf.mqttPort;
        this.ipPortformModel.username = _conf.username;
        this.ipPortformModel.pwd = _conf.pwd;

        this.captureimgFormModel.captureimgScale = _conf.captureimgScale;
        this.captureimgFormModel.captureimgQuality = _conf.captureimgQuality;
        this.captureimgFormModel.captureimgMarks = _conf.captureimgMarks;
        this.captureimgFormModel.captureimgCheck = _conf.captureimgCheck;
        this.captureimgFormModel.captureimgTime = _conf.captureimgTime;
        this.captureimgFormModel.recordId = _conf.recordId;

        this.sprayFormModel.spray = _conf.spray;
        this.sprayFormModel.identify = _conf.identify;
        this.sprayFormModel.sprayInterval = _conf.sprayInterval;
        this.sprayFormModel.recordId = _conf.recordId;

        this.heartbeatFormModel.heartIntervalTime = _conf.heartIntervalTime;
        this.heartbeatFormModel.recordId = _conf.recordId;
        this.heartbeatFormModel.dataDuration = _conf.dataDuration;
      }
    },
    checkProcess(sceneDictate, clearCallback, callback) {
      let params = {
        recordId: this.data.recordId,
        sceneDictate: sceneDictate
      };
      let result = DeviceCommandDownQueueApi.workingDetail(params);
      result
        .then(result => {
          if (!result || !result.dcdqId) { //处理完成了
            clearCallback && clearCallback(); //去掉菊花+清除循环调用
            callback && callback(); //用户自定义
          }
        })
        .catch(() => {
          clearCallback && clearCallback(); //去掉菊花+清除循环调用
        });
    },


    setCaptureimgScaleActionToCmdQueue() {
      let that = this;
      this.loading = true;
      let result = DeviceSprayCameraStaticConfigApi.edit(this.captureimgFormModel);
      result.then(result => {
        // 移除加载框
        that.loading = false;
        // 提示添加成功
        message.success(result.message);
        that.init();
      }).catch(() => {
        message.error('保存失败')
        that.loading = false;
      });
    },

    //设置域名 begin
    clearIpPortformRef() {
      this.ipPortformRefLoading = false;
      clearInterval(this.ipPortformRefTimer);
    },
    checkIpPort() {
      let that = this;
      this.checkProcess(3, this.clearIpPortformRef, function () {
        that.init();
      });
    },
    setIpPortActionToCmdQueue() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 3
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          console.log(workResult);
          if (!workResult || !workResult.dcdqId) { //处理完成了
            that.pushIpPort();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushIpPort() {
      if (!this.ipPortformModel.mqttUrl || !this.ipPortformModel.mqttPort) {
        message.error('请先填写mqttUrl和端口');
        return;
      }

      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: this.sourceConfig,
          update: this.ipPortformModel
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 3,
        sceneDictate: 3
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.ipPortformRefLoading = true; //转圈
          this.ipPortformRefTimer = setInterval(this.checkIpPort, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //设置域名 end

    //保存默认配置
    saveSprayConfig() {
      let that = this;
      this.loading = true;
      let result = DeviceSprayCameraStaticConfigApi.edit(this.sprayFormModel);
      result.then(result => {
        // 移除加载框
        that.loading = false;
        // 提示添加成功
        message.success(result.message);
        that.init();
      }).catch(() => {
        message.error('保存失败')
        that.loading = false;
      });
    },


    // 喷淋配置相关方法
    clearSprayFormRef() {
      this.sprayFormRefLoading = false;
      clearInterval(this.sprayFormRefTimer);
    },

    checkSpray() {
      let that = this;
      this.checkProcess(5, this.clearSprayFormRef, function () {
        that.init();
      });
    },

    setSprayActionToCmdQueue() {
      let that = this;
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 5
      };
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            let result1 = DeviceSprayCameraStaticConfigApi.edit(that.sprayFormModel);
            result1.then(result => {
              that.pushSpray();
            }).catch(() => {
              message.error('保存失败')
            });
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },

    pushSpray() {
      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: ''
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 5,
        sceneDictate: 5
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.sprayFormRefLoading = true;
          this.sprayFormRefTimer = setInterval(this.checkSpray, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 喷淋配置相关方法end

    //喷淋模式begin
    clearSprayModeRef() {
      this.sprayModeRefLoading = false;
      clearInterval(this.sprayModeRefTimer);
    },

    checkSprayMode() {
      let that = this;
      this.checkProcess(12, this.clearSprayModeRef, function () {
        that.spraymode = 0;
      });
    },
    setSprayModeToCmdQueue() {
      if (this.spraymode == 0) {
        message.error('请先选择喷淋模式');
        return;
      }
      let that = this;
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 12
      };
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushModeSpray();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushModeSpray() {
      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: {
            mode: this.spraymode - 1
          }
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 12,
        sceneDictate: 12
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.sprayModeRefLoading = true;
          this.sprayModeRefTimer = setInterval(this.checkSprayMode, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //喷淋模式end

    //wifi热点设置begin
    clearSpraWifiHotspotFormRef() {
      this.wifiHotspotFormRefLoading = false;
      clearInterval(this.wifiHotspotFormRefTimer);
    },

    checkSpraWifiHotspot() {
      this.checkProcess(11, this.clearSpraWifiHotspotFormRef, function () {
      });
    },
    saveWifiHotspotConfig() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 11
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushSpraWifiHotspot();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushSpraWifiHotspot() {
      if (!this.wifiHotspotFormModel.ssid || !this.wifiHotspotFormModel.pwd) {
        message.error('请先填写wifi热点名称和密码');
        return;
      }

      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: this.wifiHotspotFormModel
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 11,
        sceneDictate: 11
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.wifiHotspotFormRefLoading = true;
          this.wifiHotspotFormRefTimer = setInterval(this.checkSpraWifiHotspot, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //wifi热点设置end

    //重启begin
    clearRebootActionRef() {
      this.rebootRefLoading = false;
      clearInterval(this.rebootRefTimer);
    },

    checkRebootAction() {
      this.checkProcess(8, this.clearRebootActionRef, function () {
      });
    },
    rebootActionToCmdQueue() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 8
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushRebootAction();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushRebootAction() {
      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: ''
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 8,
        sceneDictate: 8
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.rebootRefLoading = true;
          this.rebootRefTimer = setInterval(this.checkRebootAction, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //重启end

    saveSprayActionToDb() {
      let that = this;
      this.loading = true;
      let result1 = DeviceSprayCameraStaticConfigApi.edit(that.sprayFormModel);
      result1.then(result => {
        message.success('保存成功')
        this.loading = false;
        that.init();
      }).catch(() => {
        this.loading = false;
        message.error('保存失败')
      });
    },


    //OSS配置设置begin
    clearImageUploadFormRef() {
      this.imageUploadFormRefLoading = false;
      clearInterval(this.imageUploadFormRefTimer);
    },

    checkImageUploadForm() {
      this.checkProcess(14, this.clearImageUploadFormRef, function () {
      });
    },
    saveImageUploadForm() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 14
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushImageUploadForm();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushImageUploadForm() {
      if (!this.imageUploadFormModel.mode) {
        message.error('请选择图片的上传方式');
        return;
      }
      if (this.imageUploadFormModel.mode === 'oss' && (!this.imageUploadFormModel.ossAk || !this.imageUploadFormModel.ossSk || !this.imageUploadFormModel.ossBucket || !this.imageUploadFormModel.ossFileprefix || !this.imageUploadFormModel.ossEndpoint)) {
        message.error('请完善OSS的配置');
        return;
      }

      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: this.imageUploadFormModel
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 14,
        sceneDictate: 14
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.imageUploadFormRefLoading = true;
          this.imageUploadFormRefTimer = setInterval(this.checkImageUploadForm, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //OSS配置设置end

    syncUploadConfigToSomeOxtall() {
      if (this.data.oxtId <= 0) {
        message.error('当前牛舍没有其他牛舍');
        return;
      }
      if (!this.imageUploadFormModel.mode) {
        message.error('请选择图片的上传方式');
        return;
      }
      if (this.imageUploadFormModel.mode === 'oss' && (!this.imageUploadFormModel.ossAk || !this.imageUploadFormModel.ossSk || !this.imageUploadFormModel.ossBucket || !this.imageUploadFormModel.ossFileprefix || !this.imageUploadFormModel.ossEndpoint)) {
        message.error('请完善OSS的配置');
        return;
      }
      let that = this;
      this.loading = true;
      let toObjs = {
        "oxtall_ids" : [that.data.oxtId]
      };
      let addParams = {
        "templateCode" : "spraycamera",
        "toObjs" : JSON.stringify(toObjs),
        "sceneDictate" : 14,
        "dictate" : 14,
        "templateContent" :  JSON.stringify(that.imageUploadFormModel)
      };
      let result2 = DeviceCommandTemplateDistributeApi.add(addParams);
      result2.then(res => {
        // 移除加载框
        that.loading = false;
        // 提示添加成功
        message.success(res.message);
      })
        .catch(() => {
          that.loading = false;
        });
    },


    //心跳设置开始
    clearHeartbeatFormRef() {
      this.heartbeatFormRefLoading = false;
      clearInterval(this.heartbeatFormRefTimer);
    },

    checkHeartbeatForm() {
      this.checkProcess(15, this.clearHeartbeatFormRef, function () {
      });
    },
    saveHeartbeatConfig() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 15
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushHeartbeatForm();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushHeartbeatForm() {
      if (!this.heartbeatFormModel.heartIntervalTime) {
        message.error('请输入心跳间隔时间');
        return;
      }
      if (!this.heartbeatFormModel.dataDuration) {
        message.error('请输入数据上报周期');
        return;
      }

      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: this.heartbeatFormModel
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 15,
        sceneDictate: 15
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.heartbeatFormRefLoading = true;
          this.heartbeatFormRefTimer = setInterval(this.checkHeartbeatForm, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //心跳设置结束
    syncHeartbeatConfigToSomeOxtall() {
      if (this.data.oxtId <= 0) {
        message.error('当前牛舍没有其他牛舍');
        return;
      }
      if (!this.heartbeatFormModel.heartIntervalTime) {
        message.error('请输入心跳间隔时间');
        return;
      }
      if (!this.heartbeatFormModel.dataDuration) {
        message.error('请输入数据上报周期');
        return;
      }
      let that = this;
      this.loading = true;
      let toObjs = {
        "oxtall_ids" : [that.data.oxtId]
      };
      let addParams = {
        "templateCode" : "spraycamera",
        "toObjs" : JSON.stringify(toObjs),
        "sceneDictate" : 15,
        "dictate" : 15,
        "templateContent" :  JSON.stringify(that.heartbeatFormModel)
      };
      let result2 = DeviceCommandTemplateDistributeApi.add(addParams);
      result2.then(res => {
        // 移除加载框
        that.loading = false;
        // 提示添加成功
        message.success(res.message);
      })
        .catch(() => {
          that.loading = false;
        });
    },


    //读取心跳开始
    clearReadHeartbeatFormRef() {
      this.readHeartbeatFormRefLoading = false;
      clearInterval(this.readHeartbeatFormRefTimer);
    },

    checkReadHeartbeatForm() {
      let that = this;
      this.checkProcess(16, this.clearReadHeartbeatFormRef, function () {
        that.init();
      });
    },
    readHeartbeatConfig() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 16
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushReadHeartbeatCommand();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushReadHeartbeatCommand() {
      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: ''
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 16,
        sceneDictate: 16
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.readHeartbeatFormRefLoading = true;
          this.readHeartbeatFormRefTimer = setInterval(this.checkReadHeartbeatForm, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //读取心跳结束

    //读取相机图片传输配置开始
    clearReadImageUploadFormRef() {
      this.readImageUploadFormRefLoading = false;
      clearInterval(this.readImageUploadFormRefTimer);
    },
    checkReadImageUploadForm() {
      let that = this;
      this.checkProcess(17, this.clearReadImageUploadFormRef, function () {
        that.init();
      });
    },
    readImageUploadConfig() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 17
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          if (!workResult || !workResult.dcdqId) {
            that.pushReadImageUploadCommand();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    pushReadImageUploadCommand() {
      this.loading = true;
      let updateDataRecord = {
        updateData: JSON.stringify({
          old: '',
          update: ''
        }),
        recordId: this.data.recordId,
        templateCode: 'spraycamera',
        dictate: 17,
        sceneDictate: 17
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.readImageUploadFormRefLoading = true;
          this.readImageUploadFormRefTimer = setInterval(this.checkReadImageUploadForm, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },


    //读取相机图片传输配置结束

  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
